import Course from "../models/courseModel.js";
import CourseSelection from "../models/courseSelectionModel.js";
import User from "../models/userModel.js";

export default class CoursesController {
  static async apiAddCourse(req, res, next) {
    try {
      const { coursename, creator } = req.body;
      const newCourse = new Course({
        coursename,
        creator,
      });
      await newCourse.save();
      res.status(200).json({ data: newCourse});
    } catch (e) {
      res.status(500).json({ error: e.message });
    }
  }

  static async apiGetCourses(req, res, next) {
    try {
      const userId = req.query.id;
      let courses;
      if (userId) {
        // find all course subscribe or created by userId
        const user = await User.findById(userId);
        if (user.role == 'teacher') {
          courses = await Course.find({creator: userId});
        } else {
          const data = await CourseSelection
            .find({student: userId})
            .populate({path: 'course', populate: {path: 'creator'}});
          courses = data.map(item => {return item.course});
        }
      } else {
        courses = await Course.find({}).populate('creator');
      }
      res.status(200).json(courses);
    } catch (e) {
      res.status(500).json({ error: e.message });
    }
  }

  static async apiGetCourse(req, res, next) {
    try {
      const course = await Course.findById(req.query.id);
      console.log("apiGetCourse find:", course)
      res.status(200).json(course);
    } catch (e) {
      res.status(500).json({ error: e.message });
    }
  }

  static async apiGetStudentsForCourse(req, res, next) {
    try {
      const {course} = req.body;
      // TODO: find id of all student and return their info
      const data = await CourseSelection
        .find({course: course})
        .populate('student');
      const students = data.map(item => {return item.student});
      res.status(200).json(students);
    } catch (e) {
      res.status(500).json({ error: e.message });
    }
  }

  static async apiUpdateCourse(req, res, next) {
    try {
      const {id, about} = req.body;
      const course = await Course.findByIdAndUpdate(id, {about: about}, {new: true});
      res.status(200).json({ message: 'update course: ' + id})
    } catch (e) {
      res.status(500).json({ error: e.message });
    }
  }

  static async apiSubscribeCourse(req, res, next) {
    try {
      const {userId, courseId} = req.body;
      const ob = new CourseSelection({course: courseId, student: userId});
      await ob.save();
      res.status(200).json({ message: 'student(' + userId + ') subscribe ' + courseId})
    } catch (e) {
      res.status(500).json({ error: e.message });
    }
  }

  static async apiIsSubscribe(req, res, next) {
    try {
      const {userId, courseId} = req.body;
      const data = await CourseSelection.find({student: userId, course: courseId});
      res.status(200).json({result: data ? true : false});
    } catch (e) {
      res.status(500).json({ error: e.message });
    }
  }
}
